package com.yu.shaungxuan.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yu.shaungxuan.constant.JwtClaimsConstant;
import com.yu.shaungxuan.mapper.StudentMapper;
import com.yu.shaungxuan.model.common.Result;
import com.yu.shaungxuan.model.dto.StudentLoginInfo;
import com.yu.shaungxuan.model.entity.ChoiceTeacher;
import com.yu.shaungxuan.model.entity.Student;
import com.yu.shaungxuan.properties.JwtProperties;
import com.yu.shaungxuan.service.ChoiceTeacherService;
import com.yu.shaungxuan.service.StudentService;
import com.yu.shaungxuan.utils.JwtUtil;
import com.yu.shaungxuan.utils.StudentHolder;
import com.yu.shaungxuan.utils.TeacherHolder;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * @author 33522
 * @description 针对表【student】的数据库操作Service实现
 * @createDate 2023-12-24 21:12:54
 */
@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student>
        implements StudentService {
    @Resource
    private JwtProperties jwtProperties;

    @Resource
    private ChoiceTeacherService choiceTeacherService;

    @Override
    public Result<String> login(StudentLoginInfo studentLoginInfo) {
        LambdaQueryWrapper<Student> studentLambdaQueryWrapper = new LambdaQueryWrapper<>();
        studentLambdaQueryWrapper.eq(StrUtil.isNotBlank(studentLoginInfo.getUsername()), Student::getUsername, studentLoginInfo.getUsername());
        Student student = getOne(studentLambdaQueryWrapper);
        if (student == null) {
            return Result.error("用户不存在");
        }
        String password = DigestUtils.md5DigestAsHex(studentLoginInfo.getPassword().getBytes());
        if (!password.equals(student.getPassword())) {
            return Result.error("密码错误");
        }
        //登录成功,生成token
        Map<String, Object> claims = new HashMap<>();
        claims.put(JwtClaimsConstant.STU_ID, student.getId());
        String token = JwtUtil.createJWT(
                jwtProperties.getAdminSecretKey(),
                jwtProperties.getAdminTtl(),
                claims);
        StudentHolder.saveStudent(student);
        return Result.success(token);
    }

    @Override
    public Result<String> choice(String teacherIds) {
        if (StrUtil.isBlank(teacherIds)) {
            return Result.error("教师id不能为空");
        }
        String[] teacherIdArray = teacherIds.split(",");
        Student student = StudentHolder.getStudent();
        Long studentId = student.getId();
        ChoiceTeacher choiceTeacher = new ChoiceTeacher();
        choiceTeacher.setStudentId(studentId);
        for (String teacherId : teacherIdArray) {
            Long id = Long.valueOf(teacherId);
            if (choiceTeacher.getTeacherFirstId() == null) {
                choiceTeacher.setTeacherFirstId(id);
            }
            if (choiceTeacher.getTeacherSecondId() == null) {
                choiceTeacher.setTeacherFirstId(id);
            }
            if (choiceTeacher.getTeacherFirstId() == null) {
                choiceTeacher.setTeacherFirstId(id);
            }
            if (choiceTeacher.getTeacherFourthId() == null) {
                choiceTeacher.setTeacherFirstId(id);
            }
        }
        boolean save = choiceTeacherService.save(choiceTeacher);
        if(save){
            return Result.success("选课成功");
        }else {
            return Result.error("选课失败");
        }
    }

    @Override
    public Result<String> logout() {
        Student student = StudentHolder.getStudent();
        if (student==null){
            return Result.error("未登录");
        }
        StudentHolder.removeStudent();
        return Result.success("成功退出");

    }


}




